package com.kungfu.utils;

import com.kungfu.config.UrlConfig;
import com.kungfu.entity.request.CheckParams;
import com.kungfu.entity.response.CheckResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Component
public class HttpUtils {
    @Autowired
    private UrlConfig config;

    public CheckResponse getCheckData(RestTemplate restTemplate, CheckParams params) {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(params.toString(), headers);
        return restTemplate.postForObject(config.getChargeUrl(), formEntity, CheckResponse.class);
    }

    public String syncMxItem(String url, RestTemplate restTemplate, String params) {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(params, headers);
        return restTemplate.postForObject(url, formEntity, String.class);
    }

    public String getSyncStart(RestTemplate restTemplate, String intoHospitalId, String outHospitalId) {
        String URL = config.getSyncUrl() + "?intoHospitalId={intoHospitalId}&outHospitalId={outHospitalId}";
        Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("intoHospitalId", intoHospitalId);
        uriVariables.put("outHospitalId", outHospitalId);
        return restTemplate.getForObject(URL, String.class, uriVariables);
    }


}
